package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 *  程序启动后,要求用户输入标题和正文,然后将数据保存到article表中
 */
public class Test2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入标题");
        String title = sc.nextLine();
        System.out.println("请输入正文");
        String content = sc.nextLine();
        try (Connection conn=DBUtil.getConnection()){
            String sql="INSERT INTO article(title,content) " +
                    "VALUES(?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,title);
            ps.setString(2,content);
            int num = ps.executeUpdate();
            if(num>0){
                System.out.println("添加成功");
            }else {
                System.out.println("添加失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
